package com.zjlanyun.eplus.view.report.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aihook.tableview.lib.TableDataAdapter;
import com.aihook.tableview.lib.TableFixHeaders;
import com.zjlanyun.eplus.R;
import com.zjlanyun.eplus.network.WebApi;
import com.zjlanyun.eplus.nohttp.HttpRequest;
import com.zjlanyun.eplus.nohttp.SimpleHttpListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ACTION;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_JSON_DATA;
import static com.zjlanyun.eplus.view.report.UtilConstants.MODEL_ID;
import static com.zjlanyun.eplus.view.report.UtilConstants.SQL_CONDITION;
import static com.zjlanyun.eplus.view.report.UtilConstants.TABLELAYOUT_ACTION;

/**
 * 作者：韩笑 on 2017/6/26 0026 09:16
 * <p>
 * 作用：表格布局的Fragment
 */


public class TableLayoutFragment extends Fragment {
    private static final String TAG = "TableLayoutFragment";

    private String modelId = "";//model_id
    private TableFixHeaders mTableFixHeaders;//表格
    ArrayList<ArrayList<String>> mList;//存放数据List

    public static TableLayoutFragment newInstance(String info, String type) {
        Bundle args = new Bundle();
        TableLayoutFragment fragment = new TableLayoutFragment();
        args.putString(MODEL_ID, info);
        args.putString(SQL_CONDITION, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.table_fragement, null);
        Log.d(TAG, getArguments().getString(MODEL_ID) + "/" + getArguments().getString(SQL_CONDITION));
        initView(view);
        return view;
    }

    /**
     * 初始化
     * @param view
     */
    public void initView(View view){
        mTableFixHeaders = (TableFixHeaders)view.findViewById(R.id.table);
        modelId = getArguments().getString(MODEL_ID);
        initData(TABLELAYOUT_ACTION,modelId);
    }

    /**
     * 查询数据
     * @param arg1 操作
     * @param arg0 modelid
     */
    public void initData(String arg1,String arg0) {
        HttpRequest client = new HttpRequest(getActivity(), WebApi.api());
        client.getRequest().add(KEY_ACTION, arg1);
        client.getRequest().add(MODEL_ID, arg0);
        client.send(new SimpleHttpListener<JSONObject>() {
            @Override
            public void onSucceed(int what, com.yanzhenjie.nohttp.rest.Response<JSONObject> response) {
                super.onSucceed(what, response);
                Log.d(TAG,"返回的数据:"+response.get().toString());
                parseData(response.get().toString());
            }
        });
    }

    /**
     * 解析数据，并在表格上显示
     * @param Json 后台返回的json数据
     */
    public void parseData(String Json){
        try{
            mList = new ArrayList<>();
            JSONObject jsonObject = new JSONObject(Json);
            JSONArray resultJsonArray = jsonObject.getJSONArray(KEY_JSON_DATA);
            for (int i = 0; i< resultJsonArray.length(); i++){
                ArrayList<String> arrayList = new ArrayList<>();
                JSONArray a =(JSONArray) resultJsonArray.get(i);
                for (int j = 0 ; j < a.length() ; j ++){
                    Log.d(TAG,j+"//"+a.get(j).toString());
                    arrayList.add(j,a.get(j).toString());
                }
                mList.add(arrayList);
            }
            Log.d(TAG,mList.size()+"");
            myHandle.sendEmptyMessageDelayed(1,20);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    Handler myHandle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1 ){
                TableDataAdapter tableDataAdapter = new TableDataAdapter(getActivity(), mList, 40, 16);
                mTableFixHeaders.setAdapter(tableDataAdapter);
            }
        }
    };
}
